Easy2Siksha.com
Storage classes are not just about theory; they are powerful tools that allow a programmer
to manage memory, speed, and accessibility wisely. Just like a good manager assigns tasks
smartly to employees, a good programmer assigns storage classes carefully to variables.
SECTION—C
5. Write a program using function to get a number from user and find sum of its digits.
Ans: The Story-like Explanation
Imagine one fine day, you are sitting at your desk with a notebook and pen. Suddenly, your
math teacher walks in and says:
“Take any number you like. Now, break it apart digit by digit and find the total of all those
digits.”
You smile, thinking—That’s easy! For example, if I take the number 2468, the digits are 2, 4,
6, and 8. Adding them gives 20.
This simple little game of breaking a number and adding its digits is exactly what computers
can do as well. The only difference is, the computer doesn’t have fingers to count. Instead, it
uses logic, loops, and functions.
Now, our task is to write a program in C language that asks the user for a number, breaks it
down into digits, adds those digits, and finally shows the result.
Sounds simple, right? But here’s the interesting part: we will not just write the program—
we’ll understand why and how every line works.
Breaking Down the Question
The question is asking for three things:
1. Write a program in C language.
So our final solution must be in the C programming language.
2. Use functions.
That means instead of writing everything inside the main() function, we will create a
separate function to calculate the sum of digits. Functions make our program neat,
reusable, and easy to understand.
3. Find the sum of digits of a number given by the user.
Which means:
o The user will enter a number.
o We will take that number, split its digits, and keep adding them one by one.